package com.ogawa.joypal.distributionnetwork.activity

import android.content.Context
import android.content.Intent
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.ogawa.base.base.BaseVMActivity
import com.ogawa.base.utils.SpUtil
import com.ogawa.base.utils.imageloader.ImageConfigImpl
import com.ogawa.base.utils.imageloader.ImageLoader
import com.ogawa.base.utils.isEmptyStr
import com.ogawa.joypal.distributionnetwork.R
import com.ogawa.joypal.distributionnetwork.adapter.DeviceManageAdapter
import com.ogawa.joypal.distributionnetwork.bean.BindUserDevicesRequest
import com.ogawa.joypal.distributionnetwork.bean.SelectRoomBean
import com.ogawa.joypal.distributionnetwork.viewmodel.SelectRoomViewModel
import com.ogawa.projectcommon.bean.EventRefreshDeviceManageActivity
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.projectcommon.utils.ProjectSpUtils
import com.ogawa.projectcommon.utils.ProjectUtils
import kotlinx.android.synthetic.main.activity_select_room.*


@Route(path = UrlPathConstant.ACTIVITY_DISTRIBUTIONNETWORK_SELECT_ROOM)
class SelectRoomActivity : BaseVMActivity<SelectRoomViewModel>() {
    private var mAdapter = DeviceManageAdapter()
    companion object{
        fun startSelectRoomActivity(context: Context){
            context.startActivity(Intent(context,SelectRoomActivity::class.java))
        }
    }
    override fun getLayoutId(): Int {
        return R.layout.activity_select_room
    }

    override fun initView() {
        super.initView()
        setTitle(getString(R.string.select_room))
        tv_start_experiencing.setOnClickListener {

        }
        initRv()
        initRvData()
        val projectConfigWifiDeviceBean = ProjectSpUtils.getProjectConfigWifiDeviceBean()
        ImageLoader.getInstance().loadImage(
            this, ImageConfigImpl.builder()
                .isCenterCrop(true)
                .url(projectConfigWifiDeviceBean.imageSrc)
                .imageView(chairIv)
                .build()
        )
    }


    private fun initRv(){
        id_recycler.layoutManager = GridLayoutManager(this,4)
        id_recycler.setHasFixedSize(true)
        mAdapter.setOnItemClickListener { _, _, position ->
            var selectBean = mAdapter.data[position]
            for (item in mAdapter.data){
                item.isSelect = false
            }
            selectBean.isSelect = true
            mAdapter.notifyDataSetChanged()
        }
        id_recycler.adapter = mAdapter

        tv_start_experiencing.setOnClickListener {
            var selectRoomStr = ""
            for (item in mAdapter.data){
                if (item.isSelect){
                    selectRoomStr = item.roomName
                }
            }
            if (isEmptyStr(selectRoomStr)){
                ToastUtils.showShort(getString(R.string.no_select_room))
                return@setOnClickListener
            }
            mViewModel.mBindUserDevicesRequest = BindUserDevicesRequest()
            val projectConfigWifiDeviceBean = ProjectSpUtils.getProjectConfigWifiDeviceBean()
            mViewModel.mBindUserDevicesRequest.sn = projectConfigWifiDeviceBean.deviceSN
            mViewModel.mBindUserDevicesRequest.name = selectRoomStr + "" + getString(R.string.chair)
            mViewModel.mBindUserDevicesRequest.label = selectRoomStr
            mViewModel.bindUserDevices()
        }
    }
    private fun initRvData(){
        var selectBeans = ArrayList<SelectRoomBean>()
        val stringArray = getResources().getStringArray(R.array.room_select)

        for (item in stringArray){
            selectBeans.add(SelectRoomBean().also { it.roomName = item })
        }


        mAdapter.setNewData(selectBeans)
    }

    override fun providerVMClass(): Class<SelectRoomViewModel>? {
        return SelectRoomViewModel::class.java
    }



    override fun observeVM() {
        //配网完成就直接到按摩界面
        mViewModel.mBindUserDeviceResponse.observe(this, Observer {
            LiveEventBus.get(EventRefreshDeviceManageActivity::class.java).post(EventRefreshDeviceManageActivity())
            val projectConfigWifiDeviceBean = ProjectSpUtils.getProjectConfigWifiDeviceBean()

            SpUtil.set(SpUtil.SN, projectConfigWifiDeviceBean.deviceSN)
             ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DEVICEMANAGE_MAIN)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                .navigation()
            ProjectUtils.jumpToDeviceActivity(projectConfigWifiDeviceBean.typeCode)

//            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DEVICEMANAGE_MAIN)
////                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
//                .navigation()
            finish()
        })
    }
}